package com.jt.www.biz.order_pc.service.impl;

import com.jt.www.biz.order_pc.service.InsuredInfoService;
import com.jt.www.common.service.FileService;
import com.jt.www.dao.mapper.agree.AgreeManageEntityMapper;
import com.jt.www.dao.mapper.insured.InsuredUserInfoMapper;
import com.jt.www.domain.enums.common.BusinessTypeCodeEnum;
import com.jt.www.domain.po.AgreeManageEntity;
import com.jt.www.domain.vo.InsuredUserInfoVo;
import com.jt.www.model.insured.InsuredUserInfoEntity;
import com.jt.www.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author admin_ltf
 * @create 2019-04-28 10:38
 */
@Service
public class InsuredInfoServiceImpl implements InsuredInfoService {

    @Autowired
    AgreeManageEntityMapper agreeManageEntityMapper;
    @Autowired
    FileService fileService;
    @Autowired
    private InsuredUserInfoMapper insuredUserInfoMapper;

    @Override
    public Integer addInsuredInfo(InsuredUserInfoEntity insuredInfo) {

        if (insuredInfo == null) {
            return -1;
        }
        // 应前端要求：根据名称对 被保险人 做校验，名称不能重复。产品说 可以重复，所以注释掉
        // A可以给B投保，C也可以给B投保，所以B可以重复添加为被保险人，注释此检验
        /*Boolean isrepeat = checkInsuredName(insuredInfo.getInsuredName());
        if(!isrepeat){
            return 2;
        }*/

        int num = insuredUserInfoMapper.insert(insuredInfo);

        return num;
    }

    private Boolean checkInsuredName(String insuredName) {
        // 根据被保险人名称 查询数据库
        InsuredUserInfoEntity insuredUserInfoEntity = new InsuredUserInfoEntity();
        insuredUserInfoEntity.setInsuredName(insuredName);
        insuredUserInfoEntity.setIsDeleted(false);
        int num = insuredUserInfoMapper.selectCount(insuredUserInfoEntity);
        // 大于0   说明重复，不能再次添加
        return num <= 0;
    }

    @Override
    public Integer updateInsuredInfo(InsuredUserInfoEntity insuredInfo) {

        if (insuredInfo == null) {
            return -1;
        }

        int num = insuredUserInfoMapper.updateByPrimaryKeySelective(insuredInfo);

        return num;
    }

    @Override
    public Integer deleteInsuredInfo(String id) {

        InsuredUserInfoEntity insuredUserInfoEntity = new InsuredUserInfoEntity();
        insuredUserInfoEntity.setId(Long.valueOf(id));
        insuredUserInfoEntity.setIsDeleted(true);
        insuredUserInfoEntity.setUpdateTime(DateUtils.getCurrentTime());
        int num = insuredUserInfoMapper.updateByPrimaryKeySelective(insuredUserInfoEntity);
        return num;
    }

//    @Override
//    public HashMap<String, Object> searchInfoByUserCode(String userCode, Integer page, Integer rows) {
//
//        PageHelper.startPage(page, rows);
//        InsuredUserInfoEntity t = new InsuredUserInfoEntity();
//        t.setUserCode(userCode);
//        t.setIsDeleted(false);
//        List<InsuredUserInfoEntity> infoList = insuredUserInfoMapper.select(t);
//        PageInfo pageinfo = new PageInfo(infoList);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("pageCount", pageinfo.getTotal());
//        map.put("result", infoList);
//        return map;
//    }

    @Override
    public List<InsuredUserInfoVo> searchInfoByUserCode(String userCode) {
        InsuredUserInfoEntity t = new InsuredUserInfoEntity();
        t.setUserCode(userCode);
        t.setIsDeleted(false);
        List<InsuredUserInfoEntity> infoList = insuredUserInfoMapper.select(t);
        List<InsuredUserInfoVo> infoVoList = Lists.newArrayList();
        for (InsuredUserInfoEntity insuredUserInfoEntity : infoList) {
            InsuredUserInfoVo insuredUserInfoVo = new InsuredUserInfoVo(insuredUserInfoEntity);
            if (StringUtils.isNotBlank(insuredUserInfoEntity.getOrgCertificateUrl())) {
                //生成鉴权url
                String fileUrl = fileService.getFileDownUrl(insuredUserInfoEntity.getOrgCertificateUrl(), BusinessTypeCodeEnum.I001.getCode(), String.valueOf(insuredUserInfoEntity.getId()), "0");
                insuredUserInfoVo.setOrgCertificateCheckUrl(fileUrl);
            }
            infoVoList.add(insuredUserInfoVo);
        }
        return infoVoList;
    }


    @Override
    public InsuredUserInfoVo searchSingleInfo(String id) {
        InsuredUserInfoEntity insuredUserInfoEntity = new InsuredUserInfoEntity();
        insuredUserInfoEntity.setId(Long.valueOf(id));
        insuredUserInfoEntity = insuredUserInfoMapper.selectByPrimaryKey(insuredUserInfoEntity);
        if (null == insuredUserInfoEntity) {
            return null;
        }
        InsuredUserInfoVo insuredUserInfoVo = new InsuredUserInfoVo(insuredUserInfoEntity);
        if (StringUtils.isNotBlank(insuredUserInfoEntity.getOrgCertificateUrl())) {
            //生成鉴权url
            String fileUrl = fileService.getFileDownUrl(insuredUserInfoEntity.getOrgCertificateUrl(), BusinessTypeCodeEnum.I001.getCode(), String.valueOf(insuredUserInfoEntity.getId()), "0");
            insuredUserInfoVo.setOrgCertificateCheckUrl(fileUrl);
        }
        return insuredUserInfoVo;
    }

    @Override
    public Boolean isSignAgreement(String userCode, String insuredName) throws Exception {

//        非新疆地区、
//        非学校  前端判断即可
//        已经签署全员投保协议且协议在有效期内的不能再签；
//        没有签署协议或已签署的协议失效、过期，
//        系统自动弹出签署全年投保协议页面
        /** 20201208  修改不判断地区
         Boolean result= false;
         // 第一步  首先判断地区
         result = judgingArea(userCode,insuredName);
         if(!result){
         return result;
         }*/
        // 第二步 判断是否签署协议.根据被保险人名称，查询协议管理表，查询是否存在，存在的话，是否在协议期间
        Boolean result = judgingIsSigned(userCode, insuredName);

        return result;
    }

    private Boolean judgingIsSigned(String userCode, String insuredName) throws Exception {
        Boolean result = true;
        AgreeManageEntity agreeManageEntity = new AgreeManageEntity();
        agreeManageEntity.setUserCode(userCode);
        agreeManageEntity.setUserName(insuredName);
        agreeManageEntity.setStatus(1);// 协议生效
        List<AgreeManageEntity> select = agreeManageEntityMapper.select(agreeManageEntity);
        if (select != null && select.size() > 0) {
            result = false;
        }
        return result;
    }

    private Boolean judgingArea(String userCode, String insuredName) throws Exception {
        Boolean result = true;
        InsuredUserInfoEntity insuredUserInfoEntity = new InsuredUserInfoEntity();
        insuredUserInfoEntity.setUserCode(userCode);
        insuredUserInfoEntity.setInsuredName(insuredName);
        insuredUserInfoEntity.setProvinceCode("650000");// 新疆地区
        // 需求中 被保险人是可以重复的 so是一个集合
        List<InsuredUserInfoEntity> select = insuredUserInfoMapper.select(insuredUserInfoEntity);
        if (select != null && select.size() > 0) {
            result = false;
        }
//        for(InsuredUserInfoEntity t : select){
//            if("650000".equals(t.getProvinceCode())){
//                result = false;
//                break;
//            }
//        }
        return result;
    }
}
